# @Time:   2020/12/8 16:00
# @Author: Zong Wei

from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.common.browser import Browser
from ui_automation_testing.utils.config import TEST_URL
from ui_automation_testing.utils.logger import Logger


class LoginPage(Browser):
    # Page Elements
    userName = (By.ID, "username")
    password = (By.ID, "password")
    login_button = (By.ID, "btnlogin")
    logout_button = (By.ID, "admui-signOut")
    confirm_button = (By.XPATH, "//a[@class='layui-layer-btn0']")

    def login(self, url=TEST_URL, name='07546', pwd='Michael777'):
        self.driver = Browser().set_browser()
        self.driver.get(url)
        wait = WebDriverWait(self.driver, 10)
        wait.until(ec.visibility_of_element_located(self.userName)).send_keys(name)
        wait.until(ec.visibility_of_element_located(self.password)).send_keys(pwd)
        wait.until(ec.element_to_be_clickable(self.login_button)).click()
        sleep(2)
        return self.driver

    # def logout(self):
    #     wait = WebDriverWait(self.driver, 10)
    #     wait.until(ec.element_to_be_clickable(self.logout_button)).click()
    #     wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
    #     self.driver.close()


if __name__ == '__main__':
    LoginPage().login()
